var express = require('express');
var router = express.Router();
const { md5, secretKey } = require("../util/salt");
const AdminApi = require("../api/admins");

/* GET users listing. */
router.get('/', async function (req, res, next) {
  const { page, limit } = req.query;
  const result = await AdminApi.get({ page, limit });
  res.send({
    "code": 0,
    "msg": "",
    "count": result.total,
    "data": result.rows,
  });
});
// 登录
router.get('/login', async function (req, res, next) {
  const { adminName, adminPassword } = req.query;
  let data = await AdminApi.login({ adminName, adminPassword });
  res.send(data);
});
// 注册
router.post('/reg', async function (req, res, next) {
  const { adminName, adminPassword } = req.body;
  let data = await AdminApi.reg({ adminName, adminPassword });
  // 数据库操作
  res.send(data);
});

router.patch('/', async function (req, res, next) {
  const { _id, date } = req.body;
  const data = await AdminApi.addDate({ _id, date });
  // 数据库操作
  res.send(data);
});

router.put('/:_id', async function (req, res, next) {
  const { _id } = req.params;
  const admin = req.body;
  admin.adminPassword = md5(admin.adminPassword);
  const data = await AdminApi.update({ _id }, admin);
  // 数据库操作
  res.send(data);
});

module.exports = router;
